﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniErp.Core.Filters;
using MiniErp.Entity.DataStreams.BOs;
using MiniErp.Entity.DataStreams.DOs;
using MiniErp.Entity.DataStreams.DTOs;
using MiniErp.Mysql.Data;
using MiniErp.Service.AutoMapperService;
using MiniErp.Service.JWTService.IService;
using MiniErp.Service.MysqlService.IService;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Threading.Tasks;

namespace MiniErp.Core.Controllers
{
    /// <summary>
    /// 登录注册控制层
    /// </summary>
    [Authorize(Policy = "Tourist")]
    public class LoginController : ApiControllers
    {
        private readonly ILoginService _loginService;
        private readonly ITokenService _tokensService;
        public LoginController(ILoginService loginService, ITokenService tokensService)
        {
            _loginService = loginService;
            _tokensService = tokensService;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="logins">工号以及密码实体</param>
        /// <returns></returns>
        [Route("Login")]
        [HttpPost]
        public async Task<string> Login(LoginDTO logins)
        {
            var users = await _loginService.Login(logins);
            return _tokensService.GetToken(users);
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="registers">用户信息</param>
        /// <returns></returns>
        [Route("Register")]
        [HttpPost]
        public async Task<bool> Register(RegisterDTO registers)
        {
            return await _loginService.Register(registers);
        }




    }
}
